import pandas as pd
from pyecharts.charts import *
import os
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.components import Table

def timeline_auto_play():
    df1 = pd.read_csv("数据清洗和分析/吉首省过去30天的空气质量.csv",index_col=0)
    timeline = Timeline(init_opts=opts.InitOpts(theme=ThemeType.ESSOS,
                                                width='600px',
                                                height='650px'))
    timeline.add_schema(is_auto_play=True,  # 自动播放
                        is_loop_play=True  # 循环播放
                        )
    cart = df1.columns.tolist()  #城市
    day = df1.index.tolist()  #日期
    for j in range(len(day)):
        data = df1.loc[day[j]].tolist()  #空气质量
        data1 = [i for i in zip(cart,data)]
        tit = "吉林省"+day[j]+"空气质量"
        m = (
            Map(init_opts=opts.InitOpts(theme=ThemeType.ESSOS,width='600px',height='650px'))
            .add('空气质量',data_pair=data1,maptype='吉林')
            .set_global_opts(title_opts=opts.TitleOpts(title=tit,pos_left='center',pos_top='20px'),
                            visualmap_opts=opts.VisualMapOpts(type_='color',
                                                            is_piecewise=True,
                                                                pieces=[
                                                                    {"max": 300, "min": 201, "label": "重度", "color": "#B40404"},
                                                                    {"max": 200, "min": 151, "label": "中度", "color": "#DF0101"},
                                                                    {"max": 150, "min": 101, "label": "轻度", "color": "#F78181"},
                                                                    {"max": 100, "min": 51, "label": "良", "color": "#F5A9A9"},
                                                                    {"max": 50, "min": 0, "label": "优", "color": "#FFFFCC"},
                                                                ]),
                            legend_opts=opts.LegendOpts(pos_top='10%'))
        )
        timeline.add(m, '{}年'.format(day[j]))
    return timeline

def Bar_opts():
    df1 = pd.read_csv("数据清洗和分析/吉首省未来14天的空气质量.csv",index_col=0)
    b = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.ESSOS,width='350px',height='350px'))
        .add_xaxis(df1.columns.tolist())
        .add_yaxis("空气质量",df1.loc[df1.index.tolist()[0]].tolist())
        .set_global_opts(title_opts=opts.TitleOpts(title="今天的空气质量",pos_left='center',pos_top="20px"),
                        legend_opts=opts.LegendOpts(pos_right='0',pos_top='40px'))
    )
    return b

def scatter_opts():
    df1 = pd.read_csv("数据清洗和分析/吉首省未来5天的空气质量.csv",index_col=0)
    s = (
        Scatter(init_opts=opts.InitOpts(theme=ThemeType.ESSOS,width='350px',height='350px'))
        .add_xaxis(df1.columns.tolist())
        .add_yaxis(df1.index.tolist()[0],df1.loc[df1.index.tolist()[0]].tolist())
        .add_yaxis(df1.index.tolist()[1],df1.loc[df1.index.tolist()[1]].tolist())
        .add_yaxis(df1.index.tolist()[2],df1.loc[df1.index.tolist()[2]].tolist())
        .add_yaxis(df1.index.tolist()[3],df1.loc[df1.index.tolist()[3]].tolist())
        .add_yaxis(df1.index.tolist()[4],df1.loc[df1.index.tolist()[4]].tolist())
        .set_global_opts(title_opts=opts.TitleOpts(title="未来5天的空气质量",pos_left='center',pos_top="20px"),
                            legend_opts=opts.LegendOpts(is_show=False))
    )
    return s

def pie_opts():
    df1 = pd.read_csv('数据爬虫/过去24小时.csv',index_col=0)
    index = df1.index.tolist()
    p = (
        Pie(init_opts=opts.InitOpts(theme=ThemeType.ESSOS,width='400px',height='700px'))
        .add('',[i for i in zip(df1.columns,df1.loc[index[0]])],radius='40%',center=['50%','25%'])
        .add('',[i for i in zip(df1.columns,df1.loc[index[1]])],radius='40%',center=['50%','55%'])
        .add('',[i for i in zip(df1.columns,df1.loc[index[2]])],radius='40%',center=['50%','85%'])
        .set_global_opts(title_opts=opts.TitleOpts(title="三小时空气质量变化",pos_left='center',pos_top="40px"),
                        legend_opts=opts.LegendOpts(type_='scroll',pos_right='0',pos_top='40%',orient='vertical'))
    )
    return p

if __name__ == "__main__":
    page = Page(layout=Page.DraggablePageLayout, page_title="基于Pyecharts的吉林省空气质量大屏")
    page.add(
        timeline_auto_play(),
        Bar_opts(),
        scatter_opts(),
        pie_opts()
    )
    page.render('1.html')

    os.system('1.html')